#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>//rand()
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;

int n,q,u,v,w;
int next[205],head[205],ver[205],edge[205],tot;
int dp[205][205],size[205];

inline void link(int x,int y){ver[++tot] = y , edge[tot] = w , next[tot] = head[x] , head[x] = tot;}

inline void dfs(int now,int father){
    for(int i = head[now];i;i = next[i]){
        if(ver[i] == father) continue;
        dfs(ver[i],now);
        size[now] += size[ ver[i] ] + 1;
        per(j,std::min(size[now],q),0){
            per(k,std::min(size[ ver[i] ]+1,j),1){
                dp[now][j] = std::max(dp[now][j],dp[now][j-k] + dp[ ver[i] ][k-1] + edge[i]);
            }
        }
    }
    return;
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> n >> q;
    rep(i,1,n-1){
        cin >> u >> v >> w;
        link(u,v) , link(v,u);
    }
    dfs(1,0);
    cout << dp[1][q] << "\n";
    return 0;
}